package com.llt.user.controller.user;


import com.llt.common.core.constants.HttpConstants;
import com.llt.common.core.controller.BaseController;
import com.llt.common.core.domain.LoginUserVO;
import com.llt.common.core.domain.R;
import com.llt.user.domain.user.vo.UserVO;
import com.llt.user.domain.user.dto.UserDTO;
import com.llt.user.domain.user.dto.UserUpdateDTO;
import com.llt.user.service.user.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author llt
 * @since 2024-10-23
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController extends BaseController {

    private final IUserService userService;

    /**
     * 发送验证码
     */
    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO dto) {
        return toR(userService.sendCode(dto));
    }

    /**
     * 登陆/注册
     */
    @PostMapping("/code/login")
    public R<String> codeLogin(@RequestBody UserDTO dto) {
        return R.ok(userService.codeLogin(dto.getPhone(), dto.getCode()));
    }

    /**
     * 退出登陆
     */
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(userService.logout(token));
    }

    /**
     * 获取个人信息
     */
    @GetMapping("/info")
    public R<LoginUserVO>  info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    /**
     * 个人主页详情信息
     */
    @GetMapping("/detail")
    public R<UserVO> detail() {
        return R.ok(userService.detail());
    }

    /**
     * 编辑个人信息
     */
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.edit(userUpdateDTO));
    }

    /**
     * 更新用户头像
     */
    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }

}
